package com.zhy.sunmi.utils.network;

import android.util.Log;

import com.zhy.sunmi.bean.Coupons;
import com.zhy.sunmi.bean.Goods;
import com.zhy.sunmi.bean.GoodsCate;
import com.zhy.sunmi.bean.SellGoodHistory;
import com.zhy.sunmi.bean.SpStaff;
import com.zhy.sunmi.bean.User;
import com.zhy.sunmi.response.AuthorityResponseModel;
import com.zhy.sunmi.utils.DecodeUtils;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author zhouhongyang
 */
public class RetrofitUtil {

    public static final int DEFAULT_TIMEOUT = 15;
    private Retrofit mRetrofit;
    private ApiService mApiService;
    private static RetrofitUtil mInstance;

    /**
     * 私有构造方法
     */
    private RetrofitUtil() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                Log.i("RetrofitLog", "retrofitBack = " + DecodeUtils.decodeUnicode(message));

            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();
                Request request = original.newBuilder()
//                        .header("auth", MyApplication.getToken())
                        .method(original.method(), original.body())
//                        .addHeader("Content-Type", "text/html; charset=gbk")
//                        .addHeader("Content-Type", "text/html; charset=UTF-8")
                        .build();

                return chain.proceed(request);
            }
        });
        builder.addInterceptor(loggingInterceptor);
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        mRetrofit = new Retrofit.Builder()
                .client(builder.build())

                .baseUrl("https://epos.mayfair.spxus.uk/epos/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        mApiService = mRetrofit.create(ApiService.class);
    }

    public static RetrofitUtil getInstance() {
        if (mInstance == null) {
            synchronized (RetrofitUtil.class) {
                mInstance = new RetrofitUtil();
            }
        }
        return mInstance;
    }

    public static RetrofitUtil getInstance2() {

        mInstance = new RetrofitUtil();


        return mInstance;
    }





    public void getToken(Subscriber<BaResponse<AuthorityResponseModel>> subscriber, String imei) {
        mApiService.getToken(imei)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getSpStaff(Subscriber<BaResponse<List<SpStaff>>> subscriber, String api_token) {
        mApiService.getSpStaff(api_token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getUsers(Subscriber<BaResponse<List<User>>> subscriber, String api_token) {
        mApiService.getUsers(api_token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getGoodsCate(Subscriber<BaResponse<List<GoodsCate>>> subscriber, String api_token) {
        mApiService.getGoodsCate(api_token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getGoods(Subscriber<BaResponse<List<Goods>>> subscriber, String api_token) {
        mApiService.getGoods(api_token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getCoupons(Subscriber<BaResponse<List<Coupons>>> subscriber, String api_token) {
        mApiService.getCoupons(api_token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getCoupons2(Subscriber<BaResponse<String>> subscriber, String api_token, String a) {
        mApiService.getCoupons2(api_token,a)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

}
